<template>
	<div class="cmt-container">
		<h3>发表评论:</h3>
		<textarea placeholder="请输入文本内容" maxlength="120" v-model="msg"></textarea>
		<button class="mui-btn mui-btn-primary mui-btn-block" @click="postComment" >发表评论</button>
		<div class="cmt-list">
			<div class="cmt-item" v-for="(item,index) in list">
				<div class="cmt-title">
					第{{index+1}}楼&nbsp;&nbsp;用户 : {{item.user_name}} 发表时间 : {{item.add_time|dateFormat}}
				</div>
				<div class="cmt-body">
					{{item.content}}
				</div>
			</div>
		</div>
		<button class="mui-btn mui-btn-warning mui-btn-outlined mui-btn-block" @click="getmore">加载更多</button>	
	</div>
</template>

<script>
	import {Toast} from 'mint-ui'
	export default{
		data(){
			return{
				pageindex:1,
				list:[],
				msg:''
			}
		},
		created(){
			this.getComments()
		},
		methods:{
			getComments(){
				this.$http.get('api/getcomments/'+this.id+'?pageindex='+this.pageindex).then(res=>{
					console.log(res.body.message);
					this.list=this.list.concat(res.body.message)
				})
			},
			getmore(){
				this.pageindex++;
				this.getComments();
			},
			postComment(){
				if(this.msg.trim().length==0){
					Toast("评论内容不能为空");
					return
				}
				this.$http.post('api/postcomment/'+this.id,{content:this.msg.trim()}).then(res=>{
					var cmt={
						user_name:'匿名用户',
						add_time:Date.now(),
						content:this.msg.trim()
					}
					this.list.unshift(cmt);
					this.msg="";
			   })
			}
		},
		props:["id"]
	}
</script>

<style scoped>
	.cmt-container{
		margin-top: 40px;
	}
	.cmt-container h3{
		font-size: 18px;
	}
	.cmt-container textarea{
		font-size: 14px;
		text-indent: 1em;
		margin-bottom: 0;
	}
	.cmt-container button{
		padding: 5px 0;
	}
	.cmt-container .cmt-list{
		margin: 5px 0;
	}
	.cmt-container .cmt-item{
		font-size: 13px;
	}
	.cmt-container .cmt-title{
		line-height: 30px;
		background-color: #ccc;
	}
	.cmt-container .cmt-body{
		line-height: 30px;
		text-indent: 2em;
	}
</style>
